<template>
  <div class="app-container">
    <el-row :gutter="20">

      <!--仓库选择-->
      <div class="head-container">
        <el-drawer
          title="选择仓库"
          size="18%"
          :visible.sync="drawer"
          :direction="direction"
        >
          <el-tree
            :data="deptOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            node-key="id"
            default-expand-all
            highlight-current
            @node-click="handleNodeClick"
            v-hasRole ="['admin','department']"
          />
          <el-button style="margin-top:20px;position:relative;left:25%;" type="info" icon="el-icon-delete" size="mini" @click="resetChecked">查看所有</el-button>
        </el-drawer>
      </div>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
    <el-form-item label="物品名称" prop="itemName">
      <el-input
        v-model="queryParams.itemName"
        placeholder="请输入物品名称"
        clearable
        @keyup.enter.native="handleQuery"
      />
    </el-form-item>
      <el-form-item label="操作类型" prop="operateType">
        <el-select v-model="queryParams.operateType" placeholder="请选择操作类型" clearable @keyup.enter.native="handleQuery">
          <el-option v-for="item in options_operateType" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>


    <el-form-item>
      <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
    </el-form-item>
    </el-form>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="info"
            size="mini"
            plain
            icon="el-icon-search"
            @click="drawer = true"
            v-hasRole="['department']"
          >选择仓库</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                     v-hasPermi="['warehousemanager:operate:export']">导出</el-button>
        </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    </el-row>
    <el-table v-loading="loading" :data="operateList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" width="55" type="index" :index.sync="indexMethod" />
      <el-table-column label="物品名称" align="center" prop="item.itemName" />
      <el-table-column label="物品数量" align="center" prop="itemNum" />
      <el-table-column label="计量单位" align="center" prop="item.itemUnit" />
      <el-table-column label="数据来源" width="120"  align="center" prop="sysDept.deptName" />
      <el-table-column label="操作类型" align="center" prop="operateType" >
        <template slot-scope="scope">
          <span v-if="scope.row.operateType == '0'">报修</span>
          <span v-if="scope.row.operateType == '1'">报废</span>
          <span v-if="scope.row.operateType == '2'">收回</span>
        </template>
      </el-table-column>
      <el-table-column label="操作时间" align="center" prop="operateTime" width="180">
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['warehousemanager:operate:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['warehousemanager:operate:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  <!-- 修改 -->
  <el-dialog title="修改" :visible.sync="open" width="55%" append-to-body>
    <div style="display:flex;text-align:center;justify-content:center">
      <el-form ref="form" :model="form" :rules="rules.operateRules" label-width="80px" style="width : 70%">
        <el-form-item label="物品名称" prop="itemName" >
          <el-col >
            <el-input v-model="form.itemName" placeholder="请输入物品名称" :disabled="true" />
          </el-col>
        </el-form-item>
        <el-form-item label="物品数量" prop="itemNum">
          <el-col >
            <el-input v-model.number="form.itemNum" placeholder="请输入物品数量" />
          </el-col>
        </el-form-item>
        <el-form-item label="操作时间" prop="operateTime">
          <el-date-picker clearable style="display:flex;width:auto;"
                          v-model="form.operateTime"
                          type="datetime"
                          value-format="yyyy-MM-dd hh:mm:ss"
                          placeholder="请选择操作时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="操作类型" prop="operateType">
          <el-select style="display:flex;" v-model="form.operateType" clearable placeholder="请选择" >
            <el-option
              v-for="opt in options_operateType"
              :key="opt.value"
              :label="opt.label"
              :value="opt.value"
              :span="8">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确认</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
  </div>
  </template>

<script>
import { listOperate, getOperate, delOperate, addOperate, updateOperate } from "@/api/warehousemanager/operate";
import {deptTreeSelect } from "@/api/system/user";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";


export default {
  name: "Operate",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 外库操作管理表格数据
      operateList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //中间变量
      dept:null,
      // 部门树选项
      deptOptions: undefined,
      deptName: undefined,
      defaultProps: {
        children: "children",
        label: "label"
      },
      drawer: false,
      direction: 'ltr',
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        itemName:null,
        deptId:this.$store.getters.deptId,
        operateType: null,
      },
      // 表单参数
      form: {
        operateId:null,
        itemId:null,
        itemName:null,
        itemNum: null,
        operateTime:null,
        operateType: null,
        deptId:this.$store.getters.deptId,
      },

      options_operateType: [{
        value: '0',
        label: '报修'
      }, {
        value: '1',
        label: '报废'
      },{
        value: '2',
        label: '收回'
      }],
      // 表单校验
      rules: {
        operateRules:{
          itemName: [
            { required: true, message: '请输入物品名称', trigger: 'blur' },
            { pattern: /^[A-Za-z0-9\u4e00-\u9fa5]+$/, message: '不允许输入空格等特殊符号', trigger: 'blur' },
          ],
          itemNum: [
            { required: true, message: '请输入物品数量' },
            { type: 'number', message: '数量必须为数字值'}
          ],
          operateTime: [
            { required: true, message: '请选择操作日期', trigger: 'change' }
          ],
          operateType: [
            { required: true, message: '请选择操作类型', trigger: 'change' }
          ],
        },
      }
    };
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    this.getList();
    this.getDeptTree();
  },
  methods: {
    //自增序号
    indexMethod(index) {
      return (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index + 1;
    },

    /** 查询外库操作管理列表 */
    getList() {
      this.loading = true;
      listOperate(this.queryParams).then(response => {
        this.operateList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      if((this.$store.getters.roles.includes("admin") || this.$store.getters.roles.includes("department")) ){
        deptTreeSelect().then(response => {
          this.deptOptions = response.data;
        });
      }

    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.deptId = data.id;
      this.handleQuery();
      this.drawer = false;
    },
    resetChecked() {
      this.queryParams.deptId=null;
      this.handleQuery();
      this.drawer = false;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        operateId: null,
        itemId: null,
        itemNum: null,
        operateTime: null,
        operateType: null,
        operateRemark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.item.itemName=null;
      this.queryParams.operateType=null;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.operateId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('warehousemanager/operate/export', {
        ...this.queryParams
      }, `operate_${new Date().getTime()}.xlsx`)
      console.log(...this.queryParams);
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
        this.reset();
        this.form.operateId = row.operateId;
        this.form.itemId = row.itemId;
        this.form.itemName = row.item.itemName;
        this.form.itemNum = row.itemNum;
        this.form.operateTime = row.operateTime;
        this.form.operateType = row.operateType;
        this.open = true;
        this.title = "修改外库操作管理";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
            updateOperate(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const operateIds = row.operateId || this.ids;
      this.$modal.confirm('是否确认删除外库操作管理编号为"' + operateIds + '"的数据项？').then(function() {
        return delOperate(operateIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>
